package org.dizena.restful;

import com.alibaba.fastjson2.JSONObject;
import org.dizena.utils.IdGen;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.ArrayList;
import java.util.List;
import java.util.Random;

@RestController
public class ApiTestController
{

    @GetMapping("/api/test/str")
    public String testStr()
    {
        return new String("hello");
    }

    @GetMapping("/api/test/num")
    public Integer testNum()
    {
        return 100;
    }

    @GetMapping("/api/test/obj")
    public JSONObject testObj()
    {
        JSONObject obj = new JSONObject();
        obj.put("id", "10010100861230691");
        obj.put("name", "战神");
        obj.put("age", 25);
        return obj;
    }

    @GetMapping("/api/test/ss/{id}")
    public JSONObject testIn1(@PathVariable("id") String id)
    {
        JSONObject obj = new JSONObject();
        obj.put("id", id);
        return obj;
    }

    @GetMapping("/api/test/ll/{id}")
    public JSONObject testIn2(@PathVariable("id") Long id)
    {
        JSONObject obj = new JSONObject();
        obj.put("id", id);
        return obj;
    }

    @PostMapping("/api/test/list")
    public JSONObject list()
    {
        JSONObject json = new JSONObject();
        json.put("code", 200);
        json.put("msg", "ok");
        ArrayList<String> data = new ArrayList<>();
        data.add("张三");
        data.add("李四");
        data.add("王五");
        json.put("data", data);
        return json;
    }

    @PostMapping("/api/test/swiper")
    public List<String> swiper()
    {
        ArrayList<String> data = new ArrayList<>();
        data.add("https://applet.dizena.com/image/1.jpeg");
        data.add("https://applet.dizena.com/image/2.jpeg");
        data.add("https://applet.dizena.com/image/3.jpeg");
        return data;
    }

    @GetMapping("/api/test/grid")
    public List<JSONObject> grid()
    {
        ArrayList<JSONObject> data = new ArrayList<>();
        JSONObject o1 = new JSONObject();
        o1.put("id", 1);
        o1.put("name", "美食");
        o1.put("icon", "https://applet.dizena.com/image/icon/1.png");
        JSONObject o2 = new JSONObject();
        o2.put("id", 2);
        o2.put("name", "KTV");
        o2.put("icon", "https://applet.dizena.com/image/icon/2.png");
        JSONObject o3 = new JSONObject();
        o3.put("id", 3);
        o3.put("name", "租房");
        o3.put("icon", "https://applet.dizena.com/image/icon/3.png");
        data.add(o1);
        data.add(o2);
        data.add(o3);
        data.add(o1);
        data.add(o2);
        data.add(o3);
        data.add(o1);
        data.add(o2);
        data.add(o3);
        return data;
    }

    @GetMapping("/api/test/food")
    public List<?> foodList()
    {
        ArrayList<JSONObject> data = new ArrayList<>();
        for(int i=1;i<=10;i++){
            JSONObject obj = new JSONObject();
            obj.put("id", IdGen.get().nextIdS());
            obj.put("name","XXXYYYBBBAAA");
            obj.put("tel","13812344321");
            obj.put("addr","XX省XX市区XX路XXXXXXXXXX尔特人哥特人嗯阿发热微软个人提供人");
            obj.put("times","周一至周日：10:00-21:00");
            obj.put("pic","https://applet.dizena.com/image/icon/1.png");
            data.add(obj);
        }
        return data;
    }

    @GetMapping("/api/test/colors")
    public List<?> colors()
    {
        ArrayList<String> data = new ArrayList<>();
        for (int i = 1; i <= 5; i++)
        {
            //红色
            String red;
            //绿色
            String green;
            //蓝色
            String blue;
            //生成随机对象
            Random random = new Random();
            //生成红色颜色代码
            red = Integer.toHexString(random.nextInt(256)).toUpperCase();
            //生成绿色颜色代码
            green = Integer.toHexString(random.nextInt(256)).toUpperCase();
            //生成蓝色颜色代码
            blue = Integer.toHexString(random.nextInt(256)).toUpperCase();

            //判断红色代码的位数
            red = red.length() == 1 ? "0" + red : red;
            //判断绿色代码的位数
            green = green.length() == 1 ? "0" + green : green;
            //判断蓝色代码的位数
            blue = blue.length() == 1 ? "0" + blue : blue;
            //生成十六进制颜色值
            String color = "#" + red + green + blue;
            data.add(color);
        }

        return data;
    }

}
